﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Models;

namespace Utilities
{
    public class DB : DbContext
    {
        public DbSet<Workflow> Workflows { get; set; }
        public DbSet<Condition> Conditions { get; set; }
        public DbSet<Activity> Activities { get; set; }
        public DbSet<Field> Fields { get; set; }
        public DbSet<Participant> Participants { get; set; }
        public DbSet<Receiver> Receivers { get; set; }
        public DbSet<Company> Companies { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<ActivityResult> ActivityResults { get; set; }
        public DbSet<WorkflowInstance> WorkflowInstances { get; set; }
        public DbSet<FieldInstance> FieldInstances { get; set; }

       public DbSet<ActivityInstance> ActivityInstances { get; set; }
       public DbSet<Advisor> Advisors { get; set; }
        public DbSet<Employee> Employees { get; set; }

        public DB()
        {
            Database.SetInitializer<DB>(new WorkflowInitializer());
            this.Configuration.ValidateOnSaveEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<ApplyMsg> ApplyMsg { get; set; }

        
    }

    public class WorkflowInitializer : DropCreateDatabaseIfModelChanges<DB>
    {
        protected override void Seed(DB context)
        {
            var departments = new List<Department>
            {
                new Department { Title = "IT部"},
                new Department { Title = "人力资源部" },
                new Department { Title = "行政部" },
                new Department { Title = "市场部" },
                new Department { Title = "财务部" }
            };
            departments.ForEach(s => {
                s.ModifiedTime = DateTime.Now;
                context.Departments.Add(s);
            });
            context.SaveChanges();
            Random rid = new Random();
            int start = 1; int end = 5;
            var employees = new List<Employee>
            {
                new Employee { FullName  = "Carson",  Gender = "男", Contact="13924958372", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Meredith", Gender = "男", Contact="13959684732", DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Arturo",   Gender = "男", Contact="13924966896", DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Gytis",    Gender = "女", Contact="13924787872", DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Yan",      Gender = "女", Contact="13924097866", DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Peggy",    Gender = "男", Contact="13967866788", DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Laura",    Gender = "男", Contact="13978975673", DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Nino",     Gender = "女", Contact="13966786833", DepartmentID=rid.Next(start,end) },
                new Employee { FullName  = "Carson1",  Gender = "男", Contact="13924958372", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Meredith1", Gender = "男", Contact="13959684732", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Arturo1",   Gender = "男", Contact="13924966896", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Gytis1",    Gender = "女", Contact="13924787872", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Yan1",      Gender = "女", Contact="13924097866", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Peggy1",    Gender = "男", Contact="13967866788", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Laura1",    Gender = "男", Contact="13978975673", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Bing",     Gender = "女", Contact="13966786833", DepartmentID=rid.Next(start,end) },
                new Employee { FullName  = "Ning",  Gender = "男", Contact="13924958372" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Sheng", Gender = "男", Contact="13959684732" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Arturo",   Gender = "男", Contact="13924966896" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Gytis",    Gender = "女", Contact="13924787872" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Yan",      Gender = "女", Contact="13924097866" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Peggy",    Gender = "男", Contact="13967866788" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Laura",    Gender = "男", Contact="13978975673", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Nina",     Gender = "女", Contact="13966786833" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName  = "Fello",  Gender = "男", Contact="13924958372" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Lucas", Gender = "男", Contact="13959684732" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Tank",   Gender = "男", Contact="13924966896", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Pen",    Gender = "女", Contact="13924787872" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Feel",      Gender = "女", Contact="13924097866" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Eva",    Gender = "男", Contact="13967866788" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Ama",    Gender = "男", Contact="13978975673" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Lining",     Gender = "女", Contact="13966786833" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName  = "Liping",  Gender = "男", Contact="13924958372" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Meredith", Gender = "男", Contact="13959684732" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Huangpei",   Gender = "男", Contact="13924966896" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Adan",    Gender = "女", Contact="13924787872" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Yang",      Gender = "女", Contact="13924097866" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Shuhua",    Gender = "男", Contact="13967866788" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Orange",    Gender = "男", Contact="13978975673" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Apple",     Gender = "女", Contact="13966786833" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName  = "Abode",  Gender = "男", Contact="13924958372" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Cris", Gender = "男", Contact="13959684732" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Yulai",   Gender = "男", Contact="13924966896" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Bin",    Gender = "女", Contact="13924787872" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Stone",      Gender = "女", Contact="13924097866" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Sean",    Gender = "男", Contact="13967866788", DepartmentID=rid.Next(start,end) },
                new Employee { FullName = "Alex",    Gender = "男", Contact="13978975673" , DepartmentID=rid.Next(start,end)},
                new Employee { FullName = "Liam",     Gender = "男", Contact="13966786833" , DepartmentID=rid.Next(start,end)},
            };
            employees.ForEach(s =>{
                s.ModifiedTime = DateTime.Now;
                context.Employees.Add(s);
            } );
            context.SaveChanges();

          
        }
    }
}